/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Datos.Conexion;
import Datos.Global;
import Killer.jImputBox;
import Killer.jMessageFlush;
import Negocio.clsProveedor_insumo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author RODRIGO
 */
public class frmRubro extends javax.swing.JDialog {

    //  Conexion conn = new Conexion();
    DefaultTableModel tbModelo_insumo, tbModelo_precios;
    Object[] lista;
    int fil, idInsumo;
    String rubro;

    public frmRubro(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        tbModelo_insumo = (DefaultTableModel) tbInsumo.getModel();
        tbModelo_precios = (DefaultTableModel) tbLista_insumos.getModel();
        this.setLocationRelativeTo(null);
        this.setTitle("Agregar Rubro");
        this.cargarArbol();
        this.cargarTabla_insumoPrecio();

        lista = new Object[5];

        jTree1.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                if (node.isLeaf()) {
                    TreePath path = e.getPath();
                    Object[] nodos = path.getPath();
                    String nombreCategoria = nodos[nodos.length - 1].toString();

                    this.cargarTabla_insumo(nombreCategoria);
                }
            }

            private void cargarTabla_insumo(String nombreCategoria) {
                Global.limpiarTabla(tbInsumo);
                try {
                    String sql = "CALL `obtener_listaInsumos` (" + '"' + nombreCategoria + '"' + ")";
                    ResultSet res = Conexion.executeQuery(Conexion.conn, sql, null);
                    while (res.next()) {
                        Object[] insumo = new Object[3];
                        insumo[0] = res.getInt(1);
                        insumo[1] = res.getString(2);
                        insumo[2] = res.getString(3);
                        tbModelo_insumo.addRow(insumo);
                    }
                } catch (SQLException ex) {
                    System.out.println("error al cargar tabla insumo " + ex);
                }
            }
        });


        btnModificar.setEnabled(false);
        btnEliminar.setEnabled(false);
        txtInsumo.setEnabled(false);
        txtUnidad.setEnabled(false);
        txtPrecio.setEnabled(false);
        txtCantidad.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel1 = new org.edisoncor.gui.panel.Panel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbInsumo = new javax.swing.JTable();
        btnAceptar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbLista_insumos = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        txtInsumo = new javax.swing.JTextField();
        txtPrecio = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        txtUnidad = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        txtBuscador = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                cerrarVentanaRubro(evt);
            }
        });

        panel1.setColorPrimario(new java.awt.Color(255, 255, 153));
        panel1.setColorSecundario(new java.awt.Color(255, 255, 153));

        tbInsumo.setBackground(new java.awt.Color(102, 255, 102));
        tbInsumo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idInsumo", "Insumo", "unidad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbInsumo.setSelectionBackground(new java.awt.Color(0, 102, 0));
        tbInsumo.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbInsumo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbInsumoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbInsumo);
        tbInsumo.getColumnModel().getColumn(0).setMinWidth(0);
        tbInsumo.getColumnModel().getColumn(0).setPreferredWidth(0);
        tbInsumo.getColumnModel().getColumn(0).setMaxWidth(0);
        tbInsumo.getColumnModel().getColumn(1).setPreferredWidth(180);
        tbInsumo.getColumnModel().getColumn(2).setMinWidth(0);
        tbInsumo.getColumnModel().getColumn(2).setPreferredWidth(0);
        tbInsumo.getColumnModel().getColumn(2).setMaxWidth(0);

        btnAceptar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/icon_accept.gif"))); // NOI18N
        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnModificar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/action_refresh.gif"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        jScrollPane3.setBackground(new java.awt.Color(255, 255, 153));

        jTree1.setBackground(new java.awt.Color(255, 255, 153));
        jTree1.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jTree1.setRowHeight(20);
        jScrollPane3.setViewportView(jTree1);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        tbLista_insumos.setBackground(new java.awt.Color(102, 255, 102));
        tbLista_insumos.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        tbLista_insumos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "idInsumo", "Insumo", "Precio (bs)", "Cantidad", "Unidad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbLista_insumos.setRowHeight(20);
        tbLista_insumos.setSelectionBackground(new java.awt.Color(0, 102, 0));
        tbLista_insumos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tbLista_insumos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLista_insumosMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tbLista_insumos);
        tbLista_insumos.getColumnModel().getColumn(0).setMinWidth(0);
        tbLista_insumos.getColumnModel().getColumn(0).setPreferredWidth(0);
        tbLista_insumos.getColumnModel().getColumn(0).setMaxWidth(0);
        tbLista_insumos.getColumnModel().getColumn(1).setPreferredWidth(140);
        tbLista_insumos.getColumnModel().getColumn(2).setPreferredWidth(35);
        tbLista_insumos.getColumnModel().getColumn(3).setPreferredWidth(35);
        tbLista_insumos.getColumnModel().getColumn(4).setPreferredWidth(40);

        btnEliminar.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/page_cross.gif"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        txtInsumo.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N

        txtPrecio.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtPrecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPrecioKeyTyped(evt);
            }
        });

        txtCantidad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantidadKeyTyped(evt);
            }
        });

        txtUnidad.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Criterio de Busqueda", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Cambria", 0, 12))); // NOI18N

        txtBuscador.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        txtBuscador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscadorKeyReleased(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/lupa.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtBuscador, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtBuscador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
        panel1.setLayout(panel1Layout);
        panel1Layout.setHorizontalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnModificar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnAceptar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addComponent(txtInsumo, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panel1Layout.setVerticalGroup(
            panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(panel1Layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 448, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 447, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtInsumo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtUnidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAceptar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbInsumoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbInsumoMouseClicked
        btnEliminar.setEnabled(false);
        btnModificar.setEnabled(false);
        btnAceptar.setEnabled(true);

        int fila = tbInsumo.rowAtPoint(evt.getPoint());
        int idIns = (int) tbModelo_insumo.getValueAt(fila, 0);
        String nombre_insumo = (String) tbModelo_insumo.getValueAt(fila, 1);
        if (tbLista_insumos.getRowCount() == 0 || !existeEnListaIngredientes(idIns)) {

            String unidad = (String) tbModelo_insumo.getValueAt(fila, 2);
            jImputBox.showBox(nombre_insumo, "Asignar cantidad del insumo en (" + '"' + unidad + '"' + ")", "Asignar precio Unitario");
            if (jImputBox.btnAceptar) {
                String precio_insumo = jImputBox.txtPrecio;
                String cantidad_insumo = jImputBox.txtCantidad;
                idInsumo = (int) tbModelo_insumo.getValueAt(fila, 0);
                lista[0] = idInsumo;
                lista[1] = nombre_insumo;
                lista[2] = precio_insumo;
                lista[3] = cantidad_insumo;
                lista[4] = unidad;
                tbModelo_precios.addRow(lista);
                rubro = clsProveedor_insumo.obtener_rubro(idInsumo);
                clsProveedor_insumo.insertarProveedor_insumo(frmProveedor.idProveedor, idInsumo, rubro, Float.valueOf(String.valueOf(precio_insumo)), Float.valueOf(String.valueOf(cantidad_insumo)));
            }
        } else {
            jMessageFlush.showMessageFlush("El insumo " + nombre_insumo + " ya 'EXISTE'", jMessageFlush.shortTime, jMessageFlush.Warning);
        }

    }//GEN-LAST:event_tbInsumoMouseClicked

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed

        clsProveedor_insumo.eliminarProveedor_insumo(idInsumo);
        tbModelo_precios.removeRow(fil);
        btnEliminar.setEnabled(false);
        btnModificar.setEnabled(false);
        btnAceptar.setEnabled(true);
        txtInsumo.setText("");
        txtPrecio.setText("");
        txtCantidad.setText("");
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void tbLista_insumosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLista_insumosMouseClicked
        fil = tbLista_insumos.rowAtPoint(evt.getPoint());
        idInsumo = (int) tbModelo_precios.getValueAt(fil, 0);
        String insumo = String.valueOf(tbLista_insumos.getValueAt(fil, 1));
        String precio = String.valueOf(tbLista_insumos.getValueAt(fil, 2));
        String cantidad = String.valueOf(tbLista_insumos.getValueAt(fil, 3));
        String unidad = String.valueOf(tbLista_insumos.getValueAt(fil, 4));
        txtInsumo.setText(insumo);
        txtPrecio.setText(precio);
        txtCantidad.setText(cantidad);
        txtUnidad.setText(unidad);
        txtPrecio.setEnabled(true);
        txtCantidad.setEnabled(true);
        btnEliminar.setEnabled(true);
        btnModificar.setEnabled(true);
        btnAceptar.setEnabled(false);


    }//GEN-LAST:event_tbLista_insumosMouseClicked

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed

        float precio = Float.parseFloat(txtPrecio.getText());
        float cantidad = Float.parseFloat(txtCantidad.getText());
        clsProveedor_insumo.modificarProveedor_insumo(idInsumo, precio, cantidad);
        this.cargarTabla_insumoPrecio();
        btnAceptar.setEnabled(true);
        btnModificar.setEnabled(false);
        btnEliminar.setEnabled(false);
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        for (int i = 0; i < tbLista_insumos.getRowCount(); i++) {
            String rubros = clsProveedor_insumo.obtener_rubro((int) tbModelo_precios.getValueAt(i, 0));
            frmProveedor.listaRubros.add(rubros);
        }
        this.dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void cerrarVentanaRubro(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_cerrarVentanaRubro
        for (int i = 0; i < tbLista_insumos.getRowCount(); i++) {
            String rubros = clsProveedor_insumo.obtener_rubro((int) tbModelo_precios.getValueAt(i, 0));
            frmProveedor.listaRubros.add(rubros);
        }

    }//GEN-LAST:event_cerrarVentanaRubro

    private void txtPrecioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioKeyTyped
        Global.validarNumero(9, evt, txtPrecio);
    }//GEN-LAST:event_txtPrecioKeyTyped

    private void txtCantidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyTyped
        Global.validarNumero(9, evt, txtCantidad);
    }//GEN-LAST:event_txtCantidadKeyTyped

    private void txtBuscadorKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscadorKeyReleased
        String nombInsumo = txtBuscador.getText();
        String sql = "CALL `buscador_Insumo`(" + '"' + nombInsumo + '"' + "," + "''" + "," + 0 + ")";
        clsProveedor_insumo.mostrarTablaInsumos(tbInsumo, sql);
    }//GEN-LAST:event_txtBuscadorKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmRubro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmRubro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmRubro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmRubro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                frmRubro dialog = new frmRubro(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTree jTree1;
    private org.edisoncor.gui.panel.Panel panel1;
    private javax.swing.JTable tbInsumo;
    private javax.swing.JTable tbLista_insumos;
    private javax.swing.JTextField txtBuscador;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtInsumo;
    private javax.swing.JTextField txtPrecio;
    private javax.swing.JTextField txtUnidad;
    // End of variables declaration//GEN-END:variables

    private void cargarArbol() {
        //cambiamos los iconos del arbol
        DefaultTreeCellRenderer render = (DefaultTreeCellRenderer) jTree1.getCellRenderer();
        render.setLeafIcon(new ImageIcon(getClass().getResource("/imagenes/Hijo.png")));
        render.setOpenIcon(new ImageIcon(getClass().getResource("/imagenes/folder.png")));
        render.setClosedIcon(new ImageIcon(getClass().getResource("/imagenes/Nieto.png")));

        DefaultMutableTreeNode Categoria = new DefaultMutableTreeNode("Categoria");
        DefaultMutableTreeNode Padre;
        DefaultMutableTreeNode Hijo;
        DefaultMutableTreeNode Nieto;

        // cargamos el arbol desde la base de datos
        Object[][] data_padre = Conexion.getDatos("CALL `obtener_CategoriaPadre`");
        if (data_padre.length > 0) {
            for (int i = 0; i < data_padre.length; i++) {
                Padre = new DefaultMutableTreeNode(data_padre[i][0]);
                Categoria.add(Padre);
                Object[][] data_hijo = Conexion.getDatos("CALL `obtener_CategoriaHijas` (" + '"' + data_padre[i][0] + '"' + ")");
                if (data_hijo.length > 0) {
                    for (int j = 0; j < data_hijo.length; j++) {
                        Hijo = new DefaultMutableTreeNode(data_hijo[j][0]);
                        Padre.add(Hijo);
                        Object[][] data_nieto = Conexion.getDatos("CALL `obtener_CategoriaHijas` (" + '"' + data_hijo[j][0] + '"' + ")");
                        if (data_nieto.length > 0) {
                            for (int k = 0; k < data_nieto.length; k++) {
                                Nieto = new DefaultMutableTreeNode(data_nieto[k][0]);
                                Hijo.add(Nieto);
                            }
                        }
                    }
                }
            }
        }

        DefaultTreeModel modelo = new DefaultTreeModel(Categoria);
        this.jTree1.setModel(modelo);
    }

    private void cargarTabla_insumoPrecio() {
        Global.limpiarTabla(tbLista_insumos);
        try {
            String sql = "CALL `obtener_listaProveedorInsumo`(" + '"' + frmProveedor.idProveedor + '"' + ")";
            ResultSet res = Conexion.executeQuery(Conexion.conn, sql, null);
            while (res.next()) {
                Object[] insumo = new Object[5];
                insumo[0] = res.getInt(1);
                insumo[1] = res.getString(2);
                insumo[2] = res.getFloat(3);
                insumo[3] = res.getFloat(4);
                insumo[4] = res.getString(5);
                tbModelo_precios.addRow(insumo);

            }
        } catch (SQLException ex) {
            System.out.println("error al cargar tabla insumoPrecio " + ex);
        }
    }

    private boolean existeEnListaIngredientes(int idIns) {
        List<Integer>L=new ArrayList<>();
        for (int i = 0; i < tbModelo_precios.getRowCount(); i++) {
            L.add((Integer) tbModelo_precios.getValueAt(i, 0));
        }
        return L.contains(idIns);
    }
}